//
//  LoginViewController.m
//  LazyCat
//
//  Created by zhanshu on 15/9/8.
//  Copyright (c) 2015年 zhanshu. All rights reserved.
//

#import "LoginViewController.h"
#import "RegisterViewController.h"
#import "Html5ViewController.h"
@interface LoginViewController ()

@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UITextField *phoneNumTextField;
@property (weak, nonatomic) IBOutlet UITextField *pswTextField;
@end

@implementation LoginViewController
- (void)backAction:(UIButton *)btn {
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self initNav:@"登 录" doModel:YES];
    
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHident:) name:UIKeyboardWillHideNotification object:nil];
    
    _scrollView.contentSize = CGSizeMake(ScreenWidth, 170);
    
}
#pragma mark - 隐藏键盘
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}
- (IBAction)tapHidentKB:(UITapGestureRecognizer *)sender {
    [self.view endEditing:YES];
}
#pragma mark - 通知事件
- (void)keyboardWillShow:(NSNotification *)notifi {
    NSDictionary* info = [notifi userInfo];
    //kbSize即為鍵盤尺寸 (有width, height)
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;//得到鍵盤的高度
    _scrollView.frame = CGRectMake(0, 64, ScreenWidth, ScreenHeight-64-kbSize.height);
}
- (void)keyboardWillHident:(NSNotification *)notifi {
    _scrollView.frame = CGRectMake(0, 64, ScreenWidth, ScreenHeight-64);
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)btnClick:(UIButton *)sender {
    [self.view endEditing:YES];
    NSInteger tag = sender.tag;
    if (tag == 100) {
        //登录
        [self login];
    }else if (tag == 101) {
        //注册
        RegisterViewController *registerVC = [[RegisterViewController alloc] init];
        [self presentViewController:registerVC animated:YES completion:^{
            
        }];
    }else {
        //忘记密码
        Html5ViewController *html5 = [[Html5ViewController alloc] init];
        html5.titleStr = @"忘记密码";
        html5.requestUrl = FinePsw;
        [self presentViewController:html5 animated:YES completion:^{
            
        }];
    }
}

#pragma mark -
- (void)login {
    if (_phoneNumTextField.text.length == 0) {
        [[MessShowView share] showWithMess:@"请输入手机号"];
        return;
    }
    if (![Utils isMobileNum:_phoneNumTextField.text]) {
        [[MessShowView share] showWithMess:@"输入的手机号码有误"];
        return;
    }
    if (_pswTextField.text.length == 0) {
        [[MessShowView share] showWithMess:@"请输入密码"];
        return;
    }
    NSString *url = [NSString stringWithFormat:Login,_phoneNumTextField.text,_pswTextField.text,[Utils md5SignStr],[ShopCommonModel getShopMess].shopuser];
    [NTNetUtil sendRequestWithUrl:url requestName:nil method:NTRequestMethodGET parameters:nil block:^(NTHttpRequest *networkRequest, NSError *error) {
        NSDictionary *dic = networkRequest.responseData;
        NSString *r = dic[@"r"];
        if ([r isEqualToString:@"T"]) {
            NSDictionary *userMess = dic[@"d"];
            [AppData share].cardno = userMess[@"cardno"];
            [AppData share].email = userMess[@"email"];
            [AppData share].img = userMess[@"img"];
            [AppData share].mobile = userMess[@"mobile"];
            [AppData share].realname = userMess[@"realname"];
            [AppData share].username = userMess[@"username"];
            [AppData share].usertype = userMess[@"usertype"];
            [AppData share].usertypestr = userMess[@"usertypestr"];
            [AppData share].doLogin = YES;
            
            [AppData share].loginPhone = _phoneNumTextField.text;
            [AppData share].loginPsw = _pswTextField.text;
            
            [NSUserDefualManager storageValue:_phoneNumTextField.text forKey:UserName];
            [NSUserDefualManager storageValue:_pswTextField.text forKey:Psw];
            
            [[AppData share].baseTabVC theShoppingCartCount:userMess[@"shopcart"]];
            
            //获取个人信息
            [NTNetUtil getUserMess];
            
            [self dismissViewControllerAnimated:YES completion:^{
                
            }];
        }else {
            [[MessShowView share] showWithMess:dic[@"m"]];
        }
        
    }];
}

@end
